<!--
 * @Author: wangzm
 * @Date: 2020-01-19 16:47:13
 * @LastEditors: wangzm
 * @LastEditTime: 2020-03-11 14:28:32
 * @Description: webview
 -->
<template>
  <base-page-view class-name="page-container">
    <web-view v-if="showPage" :webview-styles="webviewStyles" :src="src" @message="getMsg"></web-view>
  </base-page-view>
</template>

<script>
import { mapState } from 'vuex'
import { mapWebView } from '@/utils/tools'
import { h5NeedLoginMap } from '@/utils/navigateTo'
import './index.scss'
export default {
  components: {
  },
  data() {
    return {
      shareOption: {},
      webviewStyles: {
        progress: {
          color: '#FF3333'
        }
      },
      src: '',
      showPage: false,
      lastLoginFlag: false,
      option: {}
    }
  },
  computed: {
    ...mapState([
      'token',
      'userInfo'
    ])
  },
  created() {
  },
  onShareAppMessage(res) {
    return this.$queryShareOption(res, this.shareOption)
  },
  onLoad(option) {
    this.shareOption = {}
    this.lastLoginFlag = false
    this.option = option
  },
  onShow() {
    if (this.src && this.token && this.lastLoginFlag === false) {
      this.showPage = false
    }
    this.initData()
  },
  onReady() {
    const title = uni.getStorageSync('webViewTitle')
    if (title) {
      this.$setNavTitle(title)
    }
  },
  destroyed() {
    uni.removeStorageSync('webViewTitle')
  },
  methods: {
    initData() {
      const option = this.option
      const webViewKey = uni.getStorageSync('webViewKey')
      const webPath = mapWebView.filter(item => option.key === item.key)
      let params = decodeURIComponent(option.params)
      let str = ''
      if (this.token) {
        this.lastLoginFlag = true
        str += `userToken=${this.token}&userId=${this.userInfo.uid}`
      } else {
        if (h5NeedLoginMap.includes(params)) {
          this.$navigateTo('/pages/login/index')
          return
        }
        this.lastLoginFlag = false
        str += `logout=true`
      }
      if (option.share_code) {
        str += `${str ? '&' : ''}invite_code=${option.share_code}`
      }
      if (webViewKey !== 'https://mp.weixin.qq.com') {
        if (params.indexOf('?') > -1) {
          params += str ? `&${str}` : ''
        } else {
          params += str ? `?${str}` : ''
        }
      }

      let hostname = ''
      if (webPath.length > 0) {
        hostname = webPath[0].url
      }
      console.log(webViewKey, webPath, params)
      if (webViewKey) {
        hostname = webViewKey
      }
      if (hostname.length > 0) {
        // #ifdef H5
        this.src = hostname + params
        // #endif
        // #ifndef H5
        this.src = hostname + decodeURIComponent(params)
        // #endif
        setTimeout(() => {
          this.showPage = true
        }, 300)
      }
    },
    getMsg(e) {
      const data = e.detail.data || []
      if (data.length > 1) {
        this.shareOption = data[data.length - 2]
      }/*  else if (data.length === 1) {
        this.shareOption = data[0]
      } */
      console.log(e.detail.data, 'message')
    },
    // 分享初始化
    initShare() {
      if (this.token) {
        this.shareOption.share_url = ''
        this.hideShare()
      } else {
        this.showShare()
      }
    }
  }
}
</script>
